import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/Login.vue'
import home from '../components/Home.vue'
import welcome from '../components/Welcome.vue'
import users from '../components/user/Users.vue'
import roles from '../components/power/Roles.vue'
import rights from '../components/power/Rights.vue'
import goods from '../components/goods/Goods.vue'
import add from '../components/goods/Add.vue'
import params from '../components/goods/Params.vue'
import categories from '../components/order/Categories.vue'
import orders from '../components/order/Orders.vue'
import reports from '../components/report/Reports.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    children:[
      {path:'/',redirect: '/welcome'},
      {path:'/welcome',component: welcome},
      {path:'/users',component: users},
      {path:'/roles',component: roles},
      {path:'/rights',component: rights},
      {path:'/goods',component: goods},
      {path:'/goods/add',component: add},
      {path:'/params',component: params},
      {path:'/categories',component: categories},
      {path:'/orders',component: orders},
      {path:'/reports',component: reports}
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  //如果访问的是login页面就放行
  if(to.path == '/login') return next()
  var tokenStr = window.sessionStorage.getItem('token')
  //如果没有tokenStr 就跳转到login页面
  if(!tokenStr) return next('/login')
  return next()
})

export default router
